package com.ae.shield.common.fluid;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nullable;

public class FluidHelper {

    /**
     * json格式
     *    "fluid": "minecraft:water",
     * @param object 包含流体的json
     * @return  从json中读取到的流体，若未读取到则返回null
     */
    @Nullable
    public static Fluid deserializeFluid(JsonObject object) {
        if (JSONUtils.hasField(object, "fluid")) {
            String f = JSONUtils.getString(object, "fluid");

            Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(f));

            if (fluid == null) {
                throw new JsonSyntaxException("未知的流体 '" + f + "'");
            }
            return fluid;
        } else {
            return null;
        }
    }
}
